package com.klun.aop.SpringProxy;

import net.sf.cglib.proxy.Enhancer;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * 单例 
 */
public class CGLibProxy1 implements MethodBeforeAdvice {

    private static CGLibProxy1 instance = new CGLibProxy1();

    private CGLibProxy1() {} // 私有化构造器

    public static CGLibProxy1 getInstance() {
        return instance;
    }

    private Enhancer enhancer = new Enhancer(); // CGlib包 用来解析字节码的

    @SuppressWarnings("unchecked")
    public  <T> T getProxy(Class<T> clazz) {
        enhancer.setSuperclass(clazz);// 动态创建一个父类
        // enhancer.setCallback(this); // 设置？？？
        return (T) enhancer.create(); // 创建
    }

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("我是前置增强");
    }
}
